//index.js
//获取应用实例

const app = getApp()
//引入文件
var WxParse = require('../../wxParse/wxParse.js');

Page({
  data: {
    // nav
    nav: { item01: "亮点", item02: "详情" ,item03 :"须知"},
    navIndex: "x0",
    curIndex: "x0",
    left: "0",



    // id为产品ID
    pInfo:{},
    // pText
    pText:[],
    // storeImg
    storeImg:  "/imgs/storedf.png",
    storeYes: false,

    // 须知
    textLi01: [],
    textLi02: [],

    // 联动关联
    top0: "",
    top1: "",
    top2:"",
    scrollT:"",

    recommend:[],
    hasRecommend:false
  },
  onLoad:function(e){
    var that = this;
    var top0,top1,top2,scrollT;
    var query = wx.createSelectorQuery().in(this);
    
    // app.load()(this);

    this.setData({
      id:e.id,
      title: e.name
    })
    wx.setNavigationBarTitle({
      title: that.data.title
    })
    
    // 产品数据
    wx.request({
      url: 'https://soya.91soya.com/api/proinfo/' + e.id+"/"+app.globalData.openId,
      method: "get",
      success: function (res) {
        var resp = res.data.data;
        var you=res.data.you;
        var xuzhi=res.data.xuzhi;
        var coll=res.data.coll;
        var a = [];
        var b = [];
        for (var i = 0; i < you.length; i++) {
          a[i] = { textLi: you[i] }
          }
        for (var i = 0; i < xuzhi.length; i++) {
          b[i] = { textLi: xuzhi[i]}
        }
        that.setData({
          pInfo:{
            id: resp.pid,
            pImg: " https://soya.91soya.com/uploads/"+ resp.p_img,
            pName: resp.pname,
            yPrice: resp.y_price,
            price: resp.price,
            tImg: " https://soya.91soya.com/uploads/" +resp.t_img,
            tName:resp.tname,
            address: "上海市静安寺区万航渡路83号金航大厦16楼",
          },
          pText:a,
          storeYes:coll,
          textLi01:b,
        })
        // 设置收藏样式
        if(coll){
          that.setData({
            storeImg: "/imgs/storeAc.png",
            storeYes: true
          })
        }

        //内容解析
        var content = resp.content
        WxParse.wxParse('content', 'html', content, that, 3);
        if(that.data.content.nodes==''){
            that.setData({
              isContent: false
            })
        }else{
            that.setData({
              isContent: true
            })
        }

        for (var i = 0; i < 3; i++) {
          (function(b){
            query.select("#x"+b).boundingClientRect(function (res) {
              that.setData({
                ["top" + b]: res.top,
              })
            })
          })(i)
        }
        query.select("#scrollBox").boundingClientRect(function (res) {
          var h0 = that.data.top0 - res.top;
          var h1 = that.data.top1 - res.top;
          var h2 = that.data.top2 - res.top;
          that.setData({
            scrollT: res.top,
            h0: h0,
            h1: h1,
            h2: h2,
          })
        }).exec()
      },
      fail: function () {
        console.log("产品数据请求失败")
      }
    })
  },
  // 页面分享
  onShareAppMessage: function () {
    var that = this;
    return {
      title: that.data.title,
      // desc: '来来来一起种植牙啊',
      path: '/pages/details/details?id=' + that.data.id
    }
  },
  // 页面显示
  onShow:function(){
    var that=this;
    wx.request({
      url: 'https://soya.91soya.com/api/proinfo/' +that.data.id + "/" + app.globalData.openId,
      method: "get",
      success: function (res) {
        var coll = res.data.coll;
        // 设置收藏样式
        if (coll) {
          that.setData({
            storeImg: "/imgs/storeAc.png",
            storeYes: true
          })
        }
      }
    })
  },

  // 导航
  Nav: app.Nav(this, "0.15"),
  // 电话
  call: app.call,
  // 收藏
  store: function () {
    var that = this;
    if(app.globalData.openId){
      wx.request({
        url: 'https://soya.91soya.com/api/coll/',
        method: "POST",
        // 传产品id
        data: {
          open_id: app.globalData.openId,
          pro_id: that.data.pInfo.id,
        },
        success: function (res) {
          console.log(res)
          if(res.data==300){
            that.setData({
              //  收藏成功这边改变一下样式
              storeImg: "/imgs/storeAc.png",
              storeYes: true
            })
          }else{
            that.setData({
              //  取消收藏成功这边改变一下样式
              storeImg: "/imgs/storedf.png",
              storeYes: false
            })
          }
        
        },
        fail:function(){
          console.log("收藏请求失败")
        }
      })
    }else{
      console.log("收藏请求没有openid")
      wx.navigateTo({
        url: '/pages/login/login?back=back&id=' + that.data.id + '&title=' + that.data.title
      })
    }
    
    
  },
  order: function () {
    var that = this;
    if (app.globalData.openId) {
      wx.navigateTo({
        url: '/pages/order/order?id=' + that.data.id,
      })
    } else {
      console.log("预订请求没有openid")
      wx.navigateTo({
        url: '/pages/login/login?back=back&id=' + that.data.id + '&title=' + that.data.title
      })
    }
  },
  scroll:function(e){
    var that=this;
    var query=wx.createSelectorQuery()
    query.select("#scrollBox").scrollOffset(function (re) {
      that.setData({
        scrollTop:re.scrollTop,
      })
      var scrollTop = that.data.scrollTop;
      var h0 = that.data.h0;
      var h1 = that.data.h1;
      var h2 = that.data.h2;
      console.log(scrollTop)
      if (scrollTop >= h0 && scrollTop < h1) {
        that.setData({
          navIndex: "x0",
          left: "0%"
        })
      } else if (scrollTop >= h1 && scrollTop < h2) {
        that.setData({
          navIndex: "x1",
          left: "15%"
        })
      } else if (scrollTop >= h2) {
        that.setData({
          navIndex: "x2",
          left: "30%"
        })
      }
    }).exec()
   
  }
})
